package com.example.quanlithuchi;

import java.util.ArrayList;
import java.util.List;

import Adapter.LoaiThuChiAdapter;
import InterFaceIModel.ILoaiThuChi;
import InterFaceIModel.LoaiThuChiModel;
import Presenter.LoaiThuChiPresenter;
import View.ILoaiThuChiView;
import ViewHoder.LoaiThuChiHoder;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Loai_thuchi_activity extends Activity implements ILoaiThuChiView{

	Button btnAdd;
	Context context=this;
	ListView ListLoaiThuChi;
	LoaiThuChiAdapter adapter;
	LoaiThuChiPresenter presenter;
	int isthuchi=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loai_thuchi_activity);
		Intent preIntent=getIntent();
		isthuchi=preIntent.getIntExtra("id", 0);
		btnAdd=(Button) findViewById(R.id.btnAddLoaithuchi);
		btnAdd.setOnClickListener(onAdd);
		ListLoaiThuChi=(ListView) findViewById(R.id.listLoaiThuChi);
		ListLoaiThuChi.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				LoaiThuChiHoder hoder=(LoaiThuChiHoder) view.getTag();
				TextView txtTextView=hoder.txtLoaiThuChi;
				
				ILoaiThuChi loaiThuChi=(ILoaiThuChi) txtTextView.getTag();
				Intent intent=new Intent();
				intent.putExtra("tenloai", loaiThuChi.getTenLoai());
				intent.putExtra("idloai", loaiThuChi.getId());
				intent.putExtra("ghichu", loaiThuChi.getGhichu());
				intent.putExtra("isthuchi", getIsThuChi());
				setResult(1, intent);
				finish();
			}
		});
		presenter=new LoaiThuChiPresenter(this);
		presenter.LoadAll();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_loai_thuchi_activity, menu);
		return true;
	}
	OnClickListener onAdd=new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent intent=new Intent(context, LoaithuchiDetailActivity.class);
			
			intent.putExtra("isthuchi",isthuchi );
			startActivityForResult(intent, 1);
		}
	};
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode==1&&resultCode==1) {
			
			String tenLoai=data.getStringExtra("tenloai");
			String ghichu=data.getStringExtra("ghichu");
			int isthuchis=data.getIntExtra("isthuchi", 0);
			long id=data.getLongExtra("id", 0);
			ILoaiThuChi loaiThuChi=new LoaiThuChiModel();
			loaiThuChi.setId(id);
			loaiThuChi.setIsThucThi(isthuchis);
			loaiThuChi.setTenLoai(tenLoai);
			loaiThuChi.setGhichu(ghichu);
			adapter.add(loaiThuChi);
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void setListLoaiThuChi(List<ILoaiThuChi> list) {
		// TODO Auto-generated method stub
		if (list!=null) {
			adapter=new LoaiThuChiAdapter(context, R.layout.loaithuchi_item, list);
			ListLoaiThuChi.setAdapter(adapter);
		}else {
			list=new ArrayList<ILoaiThuChi>();
			ListLoaiThuChi.setAdapter(adapter);
		}
	}

	@Override
	public int getIsThuChi() {
		// TODO Auto-generated method stub
		return isthuchi;
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return context;
	}
  
}
